import sys
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QDialog
from PySide6.QtPrintSupport import QPrinter, QPrintDialog
from PySide6.QtGui import QPainter, QFont

class PrintableWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Printable Widget')
        layout = QVBoxLayout(self)
        button = QPushButton('Print Me!', self)
        button.clicked.connect(self.printWidget)
        layout.addWidget(button)

    def printWidget(self):
        printer = QPrinter(QPrinter.HighResolution)
        print_dialog = QPrintDialog(printer, self)

        if print_dialog.exec_() == QDialog.Accepted:
            painter = QPainter(printer)
            # 设置字体
            font = QFont('Arial', 10)
            painter.setFont(font)

            # 绘制窗口的内容
            self.render(painter)

            # 在打印内容的下方添加一些文本
            painter.drawText(100, 100, 'This is some additional text.')

            painter.end()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = PrintableWidget()
    widget.show()
    sys.exit(app.exec_())